package com.fadadianqi.logistics.admin.ticket.controller;


import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.logistics.admin.ticket.model.Prize;
import com.fadadianqi.logistics.admin.ticket.service.PrizeService;
import com.fadadianqi.commons.JSONProvider;
import com.yeqiangwei.commons.util.FormUtils;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ParamUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
public class PrizeController {

    private static final Log log = LogFactory.getLog(PrizeController.class);

    @RequestMapping("/a/ticket/ajax/prize/save")
    @ResponseBody
    public ResponseEntity<String> ajaxSave(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");

        Prize prize = new Prize();
        FormUtils.formBuilder(request, prize);
        AjaxResult rs = null;
        try {
            prize.setId(ParamUtils.getString(request, "id", null));
            prizeService.save(prize);
            rs = new AjaxResult(prize, Boolean.TRUE, null);
        } catch (Exception e) {
            rs = new AjaxResult(null, Boolean.FALSE, null);
            log.error(e.toString());
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }


    @RequestMapping("/a/ticket/prize")
    public String showList(HttpServletRequest request, ModelMap modelMap) {

        List<TypeItem> typeItems = new ArrayList<PrizeController.TypeItem>();
        typeItems.add(new TypeItem("随机中奖", Prize.TYPE_RANDOM));
        typeItems.add(new TypeItem("每次必中", Prize.TYPE_MUST));
        typeItems.add(new TypeItem("不中奖", Prize.TYPE_NOT_MUST));
        typeItems.add(new TypeItem("利润百分比随机中奖", Prize.TYPE_PROFIX_PERCENT));

        Pagination pagination = new Pagination(request, 100);
        List<Prize> prizeItems = prizeService.findAll(pagination);


        modelMap.put("prizeItems", prizeItems);
        modelMap.put("typeItems", typeItems);


        return "/admin/ticket/prize";
    }

    @Autowired
    private PrizeService prizeService;

    public class TypeItem {

        public TypeItem(String name, int type) {
            this.name = name;
            this.type = type;
        }

        private String name;

        private int type;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }

}
